Recall: 32 “general-purpose” registers, each holds 32
bits
Eg:
Add
the contents of registers $s + $t, store the result in registry $d
2^5 = 32, 5 bits to store a registry #
15
bits to encode 3 registers
leaves
17 bits to encode operation
RAM
- large amount of memory
stored away from the CPU
- data travels between RAM
& CPU on the bus
- big array of n bytes, n ~
10^9
- each cell has an address
0, …, n-1
- Each 4-byte block is a
word
- words have addresses 0, 4,
8, c, 10, 14, 18, 1c, …
Communicating with RAM – two commands
- load
- transfer a word from a
specified address to a specified register
- desired address goes into
the Memory Address Register (MAR)
- this goes out on the bus
- data at that location
comes back on the bus and goes into the Memory Data Register (MDR)
- value in MDR moved to
destination reg
Q: How does the computer know which words contain
instructions and which contain data?
A: It
doesn’t – special register called PC (program counter) holds the address of
the next instruction to run
By
convention, we guarantee that some address (say, 0) contains code,
initialize PC to 0.
Computer
runs the fetch-execute cycle:
PC
<- O
loop
IR <- MEM[PC]
decode + execute instruction in IR
end
loop
|
- IR = Instruction Register:
holds the current instruction
Q: How does a program get executed?
A:
Program called loader puts the program in memory and sets the PC to the
address of the first instruction in the program
Q: What happens when a program ends?
A:
Need to return control to the loader to set PC to the address of the next
instruction in the loader
- register $31 stores this
address
- need to set PC to $1 (jr
$31)
Example
1:
Add
the values in registers 4 & 7, store the result in $3, then return.
MIPS
machine code:
location
|
Binary
|
Hex
|
00000000
|
0000
0000 1010 0111 0001 1000 0010 0000
|
00a71820
|
00000004
|
0000
0011 1110 0000 0000 0000 0000 1000
|
03e00008
|
Add:
Jump:
Example
2:
Add 42
and 53, store sum in $3, then return
location
|
Binary
|
Hex
|
Meaning
|
00000000
00000004
00000008
0000000C
00000010
|
0000
0000 0000 0000 0010 1000 0001 0100
0000
0000 0000 0000 0000 0000 0010 1010
0000
0000 0000 0000 0011 1000 0001 0100
0000
0000 0000 0000 0000 0000 0011 0100
+
example 1 code
|
00002814
0000002A
00003814
00000034
|
lis
$5
.word
42
list
$7
.word
52
|
- lis $d: load immediate
& skip
- treat next word as an
immediate value and load it into $d, then skip to the following
instruction
- .word is a directive
- assembler, the next byte
is 42, just put 42 in the file
Assembly
Language
- replace tedious binary/hex
encodings with simple mnemonics.
- direct translation back to
binary (assembler)
- one line of assembly = one
machine instruction (word)
Example
2 Revisited:
lis
$5 ;($5 <- 42)
.word
42
lis
$7
.word
52
add
$3, $5, $7
jr
$32
|